
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>第&nbsp;2&nbsp;章&nbsp;第一个 Python 程序</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="../toc/index.html" title="Dive Into Python">
      <link rel="previous" href="../installing_python/summary.html" title="1.9.&nbsp;小结">
      <link rel="next" href="declaring_functions.html" title="2.2.&nbsp;函数声明">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<span class="thispage">第一个 Python 程序</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="../installing_python/summary.html" title="上一页: “小结”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="declaring_functions.html" title="下一页: “函数声明”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="chapter" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="odbchelper"></a>第&nbsp;2&nbsp;章&nbsp;第一个 <span class="application">Python</span> 程序
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="toc">
            <ul>
               <li><span class="section"><a href="index.html#odbchelper.divein">2.1. 概览</a></span></li>
               <li><span class="section"><a href="declaring_functions.html">2.2. 函数声明</a></span><ul>
                     <li><span class="section"><a href="declaring_functions.html#d0e4324">2.2.1. Python 和其他编程语言数据类型的比较</a></span></li>
                  </ul>
               </li>
               <li><span class="section"><a href="documenting_functions.html">2.3. 文档化函数</a></span></li>
               <li><span class="section"><a href="everything_is_an_object.html">2.4. 万物皆对象</a></span><ul>
                     <li><span class="section"><a href="everything_is_an_object.html#d0e4686">2.4.1. 模块导入的搜索路径</a></span></li>
                     <li><span class="section"><a href="everything_is_an_object.html#d0e4801">2.4.2. 何谓对象？</a></span></li>
                  </ul>
               </li>
               <li><span class="section"><a href="indenting_code.html">2.5. 代码缩进</a></span></li>
               <li><span class="section"><a href="testing_modules.html">2.6. 测试模块</a></span></li>
            </ul>
         </div>
         <div class="abstract">
            <p>大家都很清楚，其他书籍是如何一步步从编程基础讲述到构建完整的可运行程序的，但还是让我们跳过这个部分吧！
               
            </p>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h2 class="title"><a name="odbchelper.divein"></a>2.1.&nbsp;概览
                     </h2>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="abstract">
               <p>这是一个完整的、可执行的 <span class="application">Python</span> 程序。
               </p>
            </div>
            <p>它可能对您来说根本无法理解。别着急，我们将逐行地进行剖析。不过首先把代码通读一遍，看一看是否有些可以理解的内容。</p>
            <div class="example"><a name="d0e4084"></a><h3 class="title">例&nbsp;2.1.&nbsp;<tt class="filename">odbchelper.py</tt></h3>
               <p>如果您还没有下载本书附带的样例程序, 可以 <a href="http://www.woodpecker.org.cn/diveintopython/download/diveintopython-exampleszh-cn-5.4b.zip" title="Download example scripts">下载本程序和其他样例程序</a>。
               </p><pre class="programlisting"><span class='pykeyword'>
def</span> buildConnectionString(params):
    <span class='pystring'>"""Build a connection string from a dictionary of parameters.

    Returns string."""</span>
    <span class='pykeyword'>return</span> <span class='pystring'>";"</span>.join([<span class='pystring'>"%s=%s"</span> % (k, v) <span class='pykeyword'>for</span> k, v <span class='pykeyword'>in</span> params.items()])

<span class='pykeyword'>if</span> __name__ == <span class='pystring'>"__main__"</span>:
    myParams = {<span class='pystring'>"server"</span>:<span class='pystring'>"mpilgrim"</span>, \
                <span class='pystring'>"database"</span>:<span class='pystring'>"master"</span>, \
                <span class='pystring'>"uid"</span>:<span class='pystring'>"sa"</span>, \
                <span class='pystring'>"pwd"</span>:<span class='pystring'>"secret"</span> \
                }
    <span class='pykeyword'>print</span> buildConnectionString(myParams)</pre></div>
            <p>现在运行一下这个程序，看一看结果是什么。</p><a name="tip.run.windows"></a><table class="tip" border="0" summary="">
               <tr>
                  <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/tip.png" alt="提示" title="" width="24" height="24"></td>
               </tr>
               <tr>
                  <td colspan="2" align="left" valign="top" width="99%">在 Windows 的 <span class="application">ActivePython</span> <span class="acronym">IDE</span> 中，可以选择
                     <span class="guimenu">File</span>-&gt;<span class="guimenuitem"><span class="accel">R</span>un...</span> (<span><b class="shortcut"><span><b class="keycap">Ctrl</b></span>-<span class="keysym">R</span></b></span>) 来运行 <span class="application">Python</span> 程序。输出结果将显示在交互窗口中。
                  </td>
               </tr>
            </table><a name="tip.run.mac"></a><table class="tip" border="0" summary="">
               <tr>
                  <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/tip.png" alt="提示" title="" width="24" height="24"></td>
               </tr>
               <tr>
                  <td colspan="2" align="left" valign="top" width="99%">在 <span class="abbrev">Mac</span> <span class="acronym">OS</span> 的 <span class="application">Python</span> <span class="acronym">IDE</span> 中，可以选择
                     <span class="guimenu">Python</span>-&gt;<span class="guimenuitem">Run window...</span> (<span><b class="shortcut"><span><b class="keycap">Cmd</b></span>-<span class="keysym">R</span></b></span>) 来运行 <span class="application">Python</span> 程序，但首先要设置一个重要的选项。在 <span class="acronym">IDE</span> 中打开 <tt class="filename">.py</tt> 模块，点击窗口右上角的黑色三角，弹出这个模块的选项菜单，然后将 <span class="guimenuitem">Run as __main__</span> 选中。 这个设置是同模块一同保存的，所以对于每个模块您都需要这样做。
                  </td>
               </tr>
            </table><a name="tip.run.unix"></a><table class="tip" border="0" summary="">
               <tr>
                  <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/tip.png" alt="提示" title="" width="24" height="24"></td>
               </tr>
               <tr>
                  <td colspan="2" align="left" valign="top" width="99%">在 <span class="acronym">UNIX</span> 兼容的操作系统中 (包括 <span class="abbrev">Mac</span> <span class="acronym">OS</span> X)，可以通过命令行：<b class="userinput"><tt>python <tt class="filename">odbchelper.py</tt></tt></b> 运行模块。
                     
                  </td>
               </tr>
            </table>
            <div class="informalexample"><a name="odbchelper.output"></a><p><tt class="filename">odbchelper.py</tt> 的输出结果：
               </p><pre class="screen"><span class="computeroutput">server=mpilgrim;uid=sa;database=master;pwd=secret</span></pre></div>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="../installing_python/summary.html">&lt;&lt;&nbsp;小结</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<span class="thispage">1</span> <span class="divider">|</span> <a href="declaring_functions.html" title="2.2.&nbsp;函数声明">2</a> <span class="divider">|</span> <a href="documenting_functions.html" title="2.3.&nbsp;文档化函数">3</a> <span class="divider">|</span> <a href="everything_is_an_object.html" title="2.4.&nbsp;万物皆对象">4</a> <span class="divider">|</span> <a href="indenting_code.html" title="2.5.&nbsp;代码缩进">5</a> <span class="divider">|</span> <a href="testing_modules.html" title="2.6.&nbsp;测试模块">6</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="declaring_functions.html">函数声明&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>